package itsm.isperp.module.controller.sparepartOld;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.DataRequest;
import itsm.isperp.framework.domain.IUser;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseProcessController;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppDict;
import itsm.isperp.module.entity.sparepart.SptSparepartBack;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.project.ProjProjectMemberService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.service.sparepart.SptSparepartBackService;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 备件归还管理控制器
 * 
 * @author lizx
 * @date 2013-10-6
 */
@Controller
@RequestMapping("sparepart/sparepartBack/old")
public class SptSparepartBackOldController extends
    BaseProcessController<SptSparepartBack> {

  @Autowired
  protected SptSparepartBackService sptSparepartInService;

  @Autowired
  ProjProjectMemberService projProjectMemberService;

  @Autowired
  protected ProjProjectService projProjectService;

  @Override
  public SptSparepartBackService getService() {
    return sptSparepartInService;
  }

  @Override
  public String getPageTitle() {
    return "备件归还";
  }

  @Override
  public String getProceessKey() {
    return "sparepartBackProcess";
  }

  @RequestMapping("edit")
  public ModelAndView edit(String id, HttpServletRequest req) throws Exception {
    ModelAndView mv = this.getModelAndView();

    SptSparepartBack model = null;

    AppUserService appUserService = ContextHolder
        .getSpringBean("appUserService");
    AppDictService appDictService = ContextHolder
        .getSpringBean("appDictService");

    IUser user = ContextHolder.getLoginUser();
    if (StringUtils.isEmpty(id)) {
      model = new SptSparepartBack();
      model.setId(HibernateUUIDGenerator.generate());
      model.setCreator(user.getUsername());

      model.setBackDate(new Date());

      model.setBackManId(user.getUsername());
      model.setBackManName(user.getFullname());

      mv.addObject("creatorName", user.getFullname());

    } else {
      model = this.getService().findOne(id);

      mv.addObject("creatorName",
          appUserService.findByUsername(model.getCreator()).getFullname());
    }

    mv.addObject("model", model);
    mv.addObject("projects", projProjectService.findAllPublished("id", "name"));

    List<AppDict> typeKey = appDictService.findByType("SPA_BACK_TYPE");

    mv.addObject("typeKey", typeKey);

    return mv;

  }

  @RequestMapping("approverEdit")
  public ModelAndView approverEdit(String id, HttpServletRequest req)
      throws Exception {
    ModelAndView mv = this.getModelAndView();
    SptSparepartBack model = null;
    AppUserService appUserService = ContextHolder
        .getSpringBean("appUserService");
    model = this.getService().findOne(id);
    mv.addObject("creatorName",
        appUserService.findByUsername(model.getCreator()).getFullname());
    mv.addObject("model", model);
    mv.addObject("projects", projProjectService.findAllPublished("id", "name"));
    return mv;

  }
  
  @RequestMapping("backFinishEdit")
  public ModelAndView backFinishEdit(String id, HttpServletRequest req)
      throws Exception {
    ModelAndView mv = this.getModelAndView();
    SptSparepartBack model = null;
    AppUserService appUserService = ContextHolder
        .getSpringBean("appUserService");
    model = this.getService().findOne(id);
    mv.addObject("creatorName",
        appUserService.findByUsername(model.getCreator()).getFullname());
    mv.addObject("model", model);
    mv.addObject("projects", projProjectService.findAllPublished("id", "name"));
    return mv;

  }

  @Override
  @RequestMapping(value = { "manage", "" })
  public ModelAndView manage(HttpServletRequest req) {
    ModelAndView mv = super.manage(req);

    // 项目
    DataRequest dataProject = new JqGridRequest();
    dataProject.isPageable(false);
    dataProject.setSelectColumns("id", "name");
    mv.addObject("projects", projProjectService.findAll(dataProject)
        .getContent());

    return mv;
  }
}
